/*
		 Initialize and render the MenuBar when its elements are ready 
		 to be scripted.
*/

YAHOO.util.Event.onContentReady("yproducts", function () {
		/*
				Apply the "ytoolbar" class to the <H1> so that it is styled 
				like an application toolbar.
		*/

		var oH1 = document.getElementsByTagName("h1")[0];

		YAHOO.util.Dom.addClass(oH1, "ytoolbar");


		/*
				 Instantiate a Menu:  The first argument passed to the 
				 constructor is the id of the element in the page 
				 representing the Menu; the second is an object literal 
				 of configuration properties.
		*/

		var oMenu = new YAHOO.widget.Menu("yproducts", { zindex: 2 });


		/*
				 Aligns the bottom-left corner of Menu instance to the  
				 top-left corner of the Yahoo! anchor element that is 
				 its context element.
		*/

		function positionMenu() {

				oMenu.align("bl", "tl");

		}


		// "click" handler for the "Go to..." menu item

		function onGotoClick() {
		
				var sURL = window.prompt("Enter a URL: ","");

				if (sURL && sURL.length > 0) {
						
						document.location = sURL;

				}
		
		}


		/*
					Add an additional item to the Menu instance.  Unlike the 
					other items in the Menu instance, this item is added via 
					script since its functionality requires JavaScript.
		*/

		oMenu.addItem({ text: "Go to...", id: "goto", onclick: { fn: onGotoClick } });


		/*
				"beforeShow" event listener - used to position the 
				root Menu instance when it is displayed.
		*/

		oMenu.subscribe("beforeShow", function () {
		
				if (this.getRoot() == this) {

						positionMenu();

				}
		
		});


		/*
				 Call the "render" method with no arguments since the 
				 markup for this menu already exists in the pages.
		*/

		oMenu.render();


		/*
				Position the bottom-left corner of the root menu to the 
				top-left corner of the "Yahoo!" anchor element.
		*/

		oMenu.cfg.setProperty("context", ["yahoo", "bl", "tl"]);


		// "click" event handler for "Yahoo!" button

		function onYahooClick(p_oEvent) {

				// Position and display the menu
				
				positionMenu();

				oMenu.show();


				// Stop propagation and prevent the default "click" behavior

				YAHOO.util.Event.stopEvent(p_oEvent);
				
		}


		/*
				Assign a "click" event handler to the "Yahoo!" anchor that 
				will display the menu
		*/
		
		YAHOO.util.Event.addListener("yahoo", "click", onYahooClick);


		/*
				Add a "resize" event handler for the window that will 
				reposition the H1 "toolbar" to the bottom of the viewport
		*/

		YAHOO.widget.Overlay.windowResizeEvent.subscribe(positionMenu);

});